package main


/**
 告警触发端
 */
import (
	"bytes"
	"encoding/json"
	"github.com/prometheus/common/model"
	"io/ioutil"
	"log"
	"net/http"
)

func alertSend(alertMUrl string) {
	lables := model.LabelSet{}

	lables["alertname"] = "go代码的报警测试11111"
	lables["group"] = "abc"
	lables["severity"] = "2"
	lables["job"] = "node_exporter"
	anno := model.LabelSet{}
	anno["value"] = "88"
	alerts := make([]*model.Alert, 0)
	a := &model.Alert{
		Labels:       lables,
		Annotations:  anno,
		GeneratorURL: "http://localhost:9090",
	}
	alerts = append(alerts, a)
	jsonStr, _ := json.Marshal(alerts)

	req, err := http.NewRequest("POST", alertMUrl, bytes.NewBuffer(jsonStr))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Printf("[http.post.request.err][url:%v][err:%v]", alertMUrl, err)
		return
	}
	defer resp.Body.Close()

	log.Printf("response Status:%v", resp.Status)
	log.Printf("response Headers:%v", resp.Header)
	body, _ := ioutil.ReadAll(resp.Body)
	log.Printf("response Body:%v", string(body))
}

func main() {
	// 调用接口发送消息
	alertMUrl := "http://192.168.56.13:9093/api/v1/alerts"
	alertSend(alertMUrl)

}

